/***************************************************
  > Copyright (C) 2024 ==KINGYI== All rights reserved.
  > File Name: login.c
  > Author:
  > Mail: 
  > Created Time: 2024年09月20日 星期五 17时03分08秒
 ***************************************************/

#include "login.h"
int create_addr(void)
{
	int ret = 0;
	int sockfd = 0;
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
	cJSON *json = NULL;

	addr.sin_family = AF_INET;
	addr.sin_port = htons(duankou);
	addr.sin_addr.s_addr = inet_addr(ip);

	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd == -1)
	{
		perror("socket");
		exit(-1);
	}

	ret = connect(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("connect");
		exit(-1);
	}

return sockfd;
}
//注册
void enroll(int sockfd)
{
	cJSON *json =NULL;

	int ret = 0;
	char dest[1024] = {0};
	char id[10] = {"safe"};
	
	Login log;
	while(1)
	{

		memset(&log,0,sizeof(Login));

		printf("输入账户");
		scanf("%s",log.account);
		printf("输入密码");
		scanf("%s",log.password);
        log.type = 1; 
           
		json = cJSON_CreateObject();
        cJSON_AddItemToObject(json,"id",cJSON_CreateString(id));
		cJSON_AddItemToObject(json,"account",cJSON_CreateString(log.account));
		cJSON_AddItemToObject(json,"password",cJSON_CreateString(log.password));
        cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(log.type));
		char *buf = cJSON_Print(json);

		ret = send(sockfd,buf,strlen(buf),0);
		if(ret == -1)
		{
			perror("send");
			exit(-1);
		}

		ret = recv(sockfd,dest,sizeof(dest),0);
		if(ret == -1)
		{
			perror("recv");
			exit(-1);
		}

		cJSON *responseJson = cJSON_Parse(buf);

		cJSON *node = cJSON_GetObjectItem(responseJson, "res");
	  if(node == NULL)
    {
        printf("can't find\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(log.res,node->valuestring); 
    }
        printf("%s",log.res);
	if(strcmp(log.res,"succes") == 0)
	{
	   break;
     close(sockfd);
	}
	
		cJSON_Delete(responseJson);
	}



}

void regist(int sockfd)
{
   	char principal[20] ={0};
 	int ret = 0;
	char dest[1024] = {0};
	cJSON *json =NULL;
	Login log;

	char id[10] = {"safe"};
	while(1)
	{
          
		memset(&log,0,sizeof(Login));

		printf("输入账户");
		scanf("%s",log.account);
		printf("输入密码");
		scanf("%s",log.password);
        log.type = 2; 
		json = cJSON_CreateObject();
         cJSON_AddItemToObject(json,"id",cJSON_CreateString(id));
		cJSON_AddItemToObject(json,"account",cJSON_CreateString(log.account));
		cJSON_AddItemToObject(json,"password",cJSON_CreateString(log.password));
        cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(log.type));

		char *buf = cJSON_Print(json);

		ret = send(sockfd,buf,strlen(buf),0);
		if(ret == -1)
		{
			perror("send");
			exit(-1);
		}

		ret = recv(sockfd,dest,sizeof(dest),0);
		if(ret == -1)
		{
			perror("recv");
			exit(-1);
		}

		cJSON *responseJson = cJSON_Parse(buf);

		cJSON *node = cJSON_GetObjectItem(responseJson, "res");
	    if(node == NULL)
    {
        printf("can't find\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(log.res,node->valuestring); 
    }
        printf("%s",log.res);
	if(strcmp(log.res,"succes") == 0)
	{
	   break;
     close(sockfd);
	}


		cJSON_Delete(responseJson);
	}

	
}



